//
//  JXLanguageTool.swift
//  Extend
//
//  Created by shufang zou on 2018/3/16.
//  Copyright © 2018年 敬信. All rights reserved.
//

import Foundation

public class JXLanguageTool: NSObject {
    static let languageTool = JXLanguageTool.init()
    
    
    public class func language(_ string: String) -> String {
        let bundle = JXLanguageTool.bundle()
        return NSLocalizedString(string, tableName: "Localizable", bundle: bundle, value: string, comment: string)
    }
    
    static func bundle() -> Bundle {
        var language = UserDefaultsEx.getString(ConstantsEx.kLanguage_Key)
        if language.isEmpty {
            language = ConstantsEx.kChinese_Simple
        }
        let bundlePath = Bundle.main.path(forResource: language, ofType: "lproj")
        return Bundle(path: bundlePath!)!
    }
    
    public class func setNewLanguage(_ string: String) {
        let language = UserDefaultsEx.getString(ConstantsEx.kLanguage_Key)
        if string == language {
            return
        }
        switch string {
        case ConstantsEx.kChinese_Simple:
            UserDefaultsEx.saveString(ConstantsEx.kLanguage_Key, ConstantsEx.kChinese_Simple)
            break
        case ConstantsEx.kChinese_Traditional:
            UserDefaultsEx.saveString(ConstantsEx.kLanguage_Key, ConstantsEx.kChinese_Traditional)
            break
        case ConstantsEx.kEnglish_US:
            UserDefaultsEx.saveString(ConstantsEx.kLanguage_Key, ConstantsEx.kEnglish_US)
            break
        default:
            break
        }

    }
}
